Repository navigation
[Xamarin.Android.Build.Tasks] Scope CoreCLR R8 implements rule to interfaces - #13850
jonathanpeppers wants to merge 3 commits into
Conversation
The CoreCLR typemap path kept every class that implements anything:
-keep,allowshrinking class * implements **
R8 matches `implements` also against superclasses, so this rule
restricted almost every live class (MAUI: 5,269 items, 4,134 of 4,910
classes) from optimization.
Managed interface proxy selection only needs the interface edges of
classes that implement a bound Java interface. On CoreCLR,
GenerateTypeMapProguardConfiguration now writes for each retained
typemap key:
-if interface K
-keep,allowshrinking,allowobfuscation class * implements K
* `-if interface` applies the rule only when K is an interface, so
class keys do not pin all their subclasses.
* R8 matches `implements` transitively (super-interfaces and
superclasses), so implementers of an unbound interface that extends
K still match.
* R8 never prunes library interfaces (they are always live), so these
rules have effect only for program interfaces (AndroidX, Kotlin, ...).
* `allowobfuscation`: proxy selection compares interface names, not the
implementing class names. This has no effect today because the typemap
path uses -dontobfuscate.
NativeAOT output is unchanged.
`dotnet new maui` (net12, Release .aab, Pixel 7 verified):
obf/opt/shr 0.15/51.52/62.24 -> 0.02/57.06/58.02,
DEX 3,603,728 -> 3,277,708 bytes.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the change to scope CoreCLR’s interface-preservation rules.
No open issues 🎉
What changed in this PR
This PR narrows CoreCLR’s retained-typemap R8 rules to preserve bound interface relationships without blocking optimization across nearly all Java classes. NativeAOT output remains unchanged.
Changes:
- Generate conditional implementation-preservation rules for each retained interface key.
- Remove the broad CoreCLR
implements **rule and wire the scoped behavior through MSBuild. - Update test expectations and document the new rules.
| File | Description |
|---|---|
| src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/TrimmableTypeMapBuildTests.cs | Update integration assertions for CoreCLR’s conditional interface rules. |
| src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/R8Tests.cs | Verify the common configuration no longer contains implementation rules. |
| src/Xamarin.Android.Build.Tasks/Resources/proguard_typemap_coreclr.cfg | Remove the broad rule and point to generated interface rules. |
| src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.Proguard.targets | Pass the CoreCLR-specific option to the rule generator. |
| src/Microsoft.Android.Build.Tasks/Tests/Microsoft.Android.Build.Tasks.Tests/TypeMapProguardTargetsTests.cs | Update expected CoreCLR rule output. |
| src/Microsoft.Android.Build.Tasks/Tasks/GenerateTypeMapProguardConfiguration.cs | Emit conditional interface-implementation rules when enabled. |
| Documentation/guides/D8andR8.md | Explain scoped interface preservation and its effect on optimization. |
Review effort: Balanced
|
/azp run Xamarin.Android-PR |
|
No pipelines are associated with this pull request. |
|
Internal
The public CI (dnceng-public) is offline, so this internal build is the only CI result. |
|
/review |
|
@dalexsoto review |
|
✅ Android PR Reviewer completed successfully!
|
There was a problem hiding this comment.
Reviewed all seven changed files at eb651de, including the runtime interface-proxy traversal, task inheritance, CoreCLR/NativeAOT gating, and incremental inputs. No substantiated code findings to post inline. The interface guard scopes the optimization restriction without changing NativeAOT rules; the description includes before/after size measurements and device-validation results.
CI: Both returned GitHub check runs passed, but Xamarin.Android-PR reports failure for [build 16171892]((devdiv.visualstudio.com/redacted) The maintainer attributes its failures to existing issues/network errors and reports public CI offline. I could not independently verify those classifications because access to the Azure timeline was denied. No local builds or tests were run in this review.
Generated by Android PR Reviewer for #13850 · copilot · gpt61 · ~57 AIC · ⌖ ~8 AIC · ⊞ 23.1K
Comment /review to run again
dalexsoto
left a comment
There was a problem hiding this comment.
Reviewed all seven changed files at eb651de, the R8/target wiring, and the exact runtime implementation at this head and current target. The scoped interface rules and removal of the global rule share the same CoreCLR feature gate; NativeAOT and MonoVM behavior is unchanged. Runtime proxy lookup preserves key interface names while allowing intermediate implementers to be renamed. Current-target ProGuard and runtime-remapping changes remain compatible. No blocking issues found. LGTM.
Static review only; I did not run builds or tests.
…pemap-scoped-implements-rule # Conflicts: # Documentation/guides/D8andR8.md # src/Microsoft.Android.Build.Tasks/Tests/Microsoft.Android.Build.Tasks.Tests/TypeMapProguardTargetsTests.cs
|
/azp run Xamarin.Android-PR |
|
No pipelines are associated with this pull request. |
dalexsoto
left a comment
There was a problem hiding this comment.
Reviewed the current seven-file change and the merge of main, including R8 interface-retention rules, generator/header integration, affected test expectations, and CoreCLR versus MonoVM/NativeAOT gating. No actionable blocking issues found after the separate final pass. Static review only; no builds, tests, generators, or devices were run. Pending CI alone is not a finding.
Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
a69e988685d23538be3911fbc304eb9226f04960↑ MAUI app UI check (click, picker, alert, navigation, CollectionView) passed on device.
↑ R8 source reviewed: library interfaces are never pruned; program interfaces stay live through
-keep interface K.↓ Checked with one R8 version only.
↓ CI results for this revision not yet inspected.
On the CoreCLR retained-typemap R8 path (
PublishTrimmed=true,$(AndroidLinkTool)=r8),proguard_typemap_coreclr.cfghas this rule:The rule exists because managed interface proxy selection (
TrimmableTypeMap.TryMatchInterfaces) callsClass.getInterfaces()and R8 cannot see that. But R8'simplementsalso matches superclasses, so**matches nearly every class. In adotnet new mauiapp it blocked optimization of 4,117 of 4,893 live classes (5,745 items).The proxy lookup only needs the interface edges of classes that implement a bound Java interface (a retained typemap key). The
<GenerateTypeMapProguardConfiguration/>task now writes these rules for each key on CoreCLR, inproguard_project_references.cfg:-if interface Kapplies the rule only to interface keys. Without it, a class key such asandroid.app.Activitywould block optimization of all its subclasses.implementsthrough super-interfaces and superclasses, so a class that implements an unbound interface that extendsKis still kept.android.*) from a class, so the rules only have an effect for interfaces in the app's Java code (AndroidX, Kotlin, and so on).allowobfuscation: the lookup compares interface names, not implementer names. It has no effect today, because this path uses-dontobfuscate.An alternative was an unconditional rule for interface keys only, which needs an interface list from the key extractor. It was measured and was worse: it also blocks optimization of classes that implement
android.*interfaces, for no benefit.NativeAOT output does not change.
Results
Release
.aab, local SDK built from this branch after merging main, net12. "Before" puts back the old rule with an extra ProGuard file on the same SDK. These are ther8.jsonvalues that Google Play uses. "Final items" is the number of classes + fields + methods in the output DEX.dotnet new mauidotnet new mauidotnet new androiddotnet new androidnoShrinking % goes up only because
R8StatsMetadataImpldivides by the number of items in the final output, and R8 now optimizes away about 4,350 more items. The number of items that cannot be shrunk does not change (10,622 → 10,583).Validation
dotnet test Microsoft.Android.Build.Tasks.Tests.csproj --filter FullyQualifiedName~TypeMapProguard -p:_NativeAotLlvmReadObjPath=<NDK llvm-readobj>: 126 passed.Xamarin.Android.Build.Tests.dll,R8Tests: 31 passed.RetainedTypeMapRulesDriveR8(CoreCLR and NativeAOT): 2 passed.ReleaseCoreClrTrimmableTypeMap_SingleRuntimeIdentifier_PackagesLinkedOrReadyToRunTypeMapAssemblies: passed.Mono.Android.NET-Tests -c Releaseon a Pixel 7 (CoreCLR, R8, full trimming, 339-if interfacerules): 1,155 passed, 1 skipped (NativeAOT only), 0 failed.dotnet new androidanddotnet new mauiapps start on a Pixel 7 with no errors in logcat. The MAUI UI check (button click, picker, alert, page navigation, CollectionView scroll) passed.